package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityReceivingAddress;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//收获地址列表界面
public class ReceiveAddressListActivity extends BaseActivity {

    @BindView(R.id.tv_add_address)
    TextView tvAddAddress;
    @BindView(R.id.rv_address_list)
    RecyclerView rvList;
    @BindView(R.id.ll_no_data)
    LinearLayout llNodata;
    List<EntityReceivingAddress> addressList;
    CommonAdapter<EntityReceivingAddress> adapter;
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_receive_address_list;
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, ReceiveAddressListActivity.class);
        return intent;
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getReceiveAddressList("");
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        addressList = new ArrayList<>();
        adapter = new CommonAdapter<EntityReceivingAddress>(context, R.layout.item_address_list, addressList) {
            @Override
            protected void convert(ViewHolder holder, EntityReceivingAddress entityReceivingAddress, final int position) {
                holder.setText(R.id.tv_address, entityReceivingAddress.getDes());
                holder.setText(R.id.tv_door_num, entityReceivingAddress.getDoorNumber());
                holder.setText(R.id.tv_name_phone, entityReceivingAddress.getName() + "  " + entityReceivingAddress.getPhone());
                ImageView ivUpdate = holder.getView(R.id.iv_update);
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                EntityReceivingAddress address = addressList.get(position);
                startActivity(ReceiveAddressUpdateActivity.createIntent(context, address, 1));//修改地址
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    @Override
    public void initView() {
        tvAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(ReceiveAddressUpdateActivity.createIntent(context, null, 0));//新增地址
            }
        });
        rvList.setAdapter(adapter);
        rvList.setLayoutManager(new LinearLayoutManager(context));
    }

    @Override
    public void initOperator() {
    }

    @Override
    public void onGetReceiveAddressListSuccess(List<EntityReceivingAddress> list) {
        super.onGetReceiveAddressListSuccess(list);
        if (list != null && list.size() > 0) {
            llNodata.setVisibility(View.GONE);
            rvList.setVisibility(View.VISIBLE);
            addressList.clear();
            addressList.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            llNodata.setVisibility(View.VISIBLE);
            rvList.setVisibility(View.GONE);
        }
    }

    @Override
    public void onGetReceiveAddressListFail(Exception e) {
        super.onGetReceiveAddressListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
